package com.smartleanx.module.hrm.service.examine.examineFlowPerformance;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecord.ExamineRecordDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResultConfig.ExamineResultConfigDO;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineRecord.ExamineRecordMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineResultConfig.ExamineResultConfigMapper;
import com.smartleanx.module.hrm.service.risk.RiskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.smartleanx.module.hrm.controller.admin.examine.examineFlowPerformance.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineFlowPerformance.ExamineFlowPerformanceDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineFlowPerformance.ExamineFlowPerformanceMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 考核流程中间 Service 实现类
 *
 * @author
 */
@Service
@Validated
public class ExamineFlowPerformanceServiceImpl implements ExamineFlowPerformanceService {

    @Resource
    private ExamineFlowPerformanceMapper examineFlowPerformanceMapper;
    @Resource
    private ExamineRecordMapper examineRecordMapper;
    @Resource
    private ExamineResultConfigMapper examineResultConfigMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private RiskService riskService;

    @Override
    public Integer createExamineFlowPerformance(ExamineFlowPerformanceSaveReqVO createReqVO) {
        // 插入
        ExamineFlowPerformanceDO examineFlowPerformance = BeanUtils.toBean(createReqVO, ExamineFlowPerformanceDO.class);
//        if (createReqVO.getGradeIds()!=null && createReqVO.getGradeIds().size()>0)
//        {
//            String collect = createReqVO.getGradeIds().stream().map(l -> l.toString()).collect(Collectors.joining(","));
//              examineFlowPerformance.setConditionValue(collect);
//        }
        examineFlowPerformanceMapper.insert(examineFlowPerformance);
        // 返回
        return examineFlowPerformance.getId();
    }

    @Override
    public void updateExamineFlowPerformance(JSONObject param) {
        Integer id = param.getInteger("examineId");
        List<ExamineFlowPerformanceDO> flowPerformanceList = param.getList("flowPerformance", ExamineFlowPerformanceDO.class);
        examineFlowPerformanceMapper.delete(new LambdaQueryWrapper<ExamineFlowPerformanceDO>() {{
            eq(ExamineFlowPerformanceDO::getExamineId, id);
        }});
        flowPerformanceList.forEach(flowPerformance -> {
            flowPerformance.setExamineId(id);
            examineFlowPerformanceMapper.insert(flowPerformance);
        });

    }

    @Override
    public void deleteExamineFlowPerformance(Integer id,Integer filedId ) {
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(id);
        if(examineRecordDO.getIsLowPerformanceFile().contains(",")){
            examineRecordDO.setIsLowPerformanceFile(examineRecordDO.getIsLowPerformanceFile().replace(filedId+",",""));
        }else {
            examineRecordDO.setIsLowPerformanceFile(examineRecordDO.getIsLowPerformanceFile().replace(filedId+"",""));
        }

        if(ObjectUtils.isEmpty(examineRecordDO.getIsLowPerformanceFile())){
            examineRecordDO.setIsLowPerformance(1);
        }
        examineRecordMapper.updateById(examineRecordDO);
    }

    private void validateExamineFlowPerformanceExists(Integer id) {
        if (examineFlowPerformanceMapper.selectById(id) == null) {
            throw exception(EXAMINE_FLOW_PERFORMANCE_NOT_EXISTS);
        }
    }

    @Override
    public ExamineFlowPerformanceDO getExamineFlowPerformance(Integer id) {
        return examineFlowPerformanceMapper.selectById(id);
    }

    @Override
    public List<ExamineFlowPerformanceDO> getExamineFlowPerformanceList(Integer examineId) {
        List<ExamineFlowPerformanceDO> list = examineFlowPerformanceMapper.selectList(new LambdaQueryWrapper<ExamineFlowPerformanceDO>() {{
            eq(ExamineFlowPerformanceDO::getExamineId, examineId);
        }});
        list.forEach(l->{
            if(!ObjectUtils.isEmpty(l.getConditionValue())){
                List<Integer> collect = Stream.of(l.getConditionValue().split(",")).map(s->Integer.parseInt(s)).collect(Collectors.toList());
                l.setGradeIds(collect);
            }
        });

        return list;
    }

}